﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSIMIS2019.SubPages
{
    public partial class _ChangePassword : Form
    {
        public _ChangePassword()
        {
            InitializeComponent();
        }
        private bool UpdataPwd()
        {
            bool flag = false;//更新的结果
            DBHelper db = new DBHelper();
            string pass = this.text1.Text.Trim();

            try
            {
                db.OpenConnection();//打开数据库
                //SQL语句
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("UPDATE [Admin] SET [PassWord] = '{0}' WHERE [UserName] ='{1}'",
                                pass, this.button1);
                //创建SQL命令
                SqlCommand comm = new SqlCommand(sb.ToString(), db.Connection);
                //执行SQL操作
                int temp = comm.ExecuteNonQuery();//返回值大于0，修改成功
                if (temp == 1)
                {
                    flag = true;
                }
            }
            catch (Exception)
            {
                flag = false;
            }
            finally
            {
                db.CloseConnection();//关闭数据库
            }
            return flag;
        }
        #endregion

        #region 确认按钮事件
        public void button1_Click(object sender, EventArgs e)
        {
            bool flag = false;//更新结果
            //检测密码是否与登录密码一致并且检测两次输入密码是否一致
            if (UpdataPwd() && UpdataPwd())
            {
                flag = UpdataPwd();
                if (flag == true)
                {
                    MessageBox.Show("修改成功", "系统提示", MessageBoxButtons.OK);

                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败", "系统提示", MessageBoxButtons.OK);
                }
            }
        }
        #endregion
    }
}
